package com.hopes.bizz.poll.town.handler;

import cn.hutool.core.date.DateUtil;
import com.hopes.bizz.poll.common.constant.OmConstant;

import java.util.Date;

/**
 * @author lyb
 * @version 1.0
 * @description: 用于计算乡镇历史数据的开始和结束时间
 * @date 2023/9/27 14:22
 */
public class PollTownDataTimeParser {

    // 实时数据处理范围
    private static final Integer RT_DATA_SCOPE = 5;
    // 分钟数据处理范围
    private static final Integer MIN_DATA_SCOPE = 10;

    private Integer dataType;

    private Date date;

    private Date beginTime;

    private Date endTime;

    public PollTownDataTimeParser(Integer dataType) {
        this.dataType = dataType;
        this.date = new Date();
    }

    public PollTownDataTimeParser(Integer dataType, Date date) {
        this.dataType = dataType;
        this.date = date;
    }


    public void parserHis() {
        if (OmConstant.DATA_TYPE_RT.equals(dataType)) {
            Date minDate = DateUtil.beginOfMinute(date);
            int minute = DateUtil.minute(minDate);
            this.endTime = DateUtil.offsetMinute(minDate, -minute % RT_DATA_SCOPE);
            this.beginTime = DateUtil.offsetMinute(this.endTime, -RT_DATA_SCOPE);
        } else if (OmConstant.DATA_TYPE_MIN.equals(dataType)) {
            Date minDate = DateUtil.beginOfMinute(date);
            int minute = DateUtil.minute(minDate);
            this.endTime = DateUtil.offsetMinute(minDate, -minute % MIN_DATA_SCOPE);
            this.beginTime = DateUtil.offsetMinute(this.endTime, -MIN_DATA_SCOPE);
        } else if (OmConstant.DATA_TYPE_HOUR.equals(dataType)) {
            this.endTime = DateUtil.beginOfHour(date);
            this.beginTime = DateUtil.offsetHour(this.endTime, -1);
        } else if (OmConstant.DATA_TYPE_DAY.equals(dataType)) {
            this.endTime = DateUtil.beginOfDay(date);
            this.beginTime = DateUtil.offsetDay(this.endTime, -1);
        } else if (OmConstant.DATA_TYPE_MONTH.equals(dataType)) {
            this.endTime = DateUtil.beginOfMonth(date);
            this.beginTime = DateUtil.offsetMonth(this.endTime, -1);
        } else if (OmConstant.DATA_TYPE_YEAR.equals(dataType)) {
            this.endTime = DateUtil.beginOfYear(date);
            this.beginTime = DateUtil.offsetMonth(this.endTime, -12);
        }
    }

    public void parserRtd() {
        if (OmConstant.DATA_TYPE_RT.equals(dataType)) {
            this.endTime = DateUtil.beginOfMinute(date);
            this.beginTime = DateUtil.offsetMinute(this.endTime, -RT_DATA_SCOPE);
        } else if (OmConstant.DATA_TYPE_MIN.equals(dataType)) {
            this.endTime = DateUtil.beginOfMinute(date);
            this.beginTime = DateUtil.offsetMinute(this.endTime, -MIN_DATA_SCOPE);
        } else if (OmConstant.DATA_TYPE_HOUR.equals(dataType)) {
            this.endTime = DateUtil.beginOfHour(date);
            this.beginTime = endTime;
        } else if (OmConstant.DATA_TYPE_DAY.equals(dataType)) {
            this.endTime = DateUtil.beginOfDay(date);
            this.beginTime = endTime;
        } else if (OmConstant.DATA_TYPE_MONTH.equals(dataType)) {
            this.endTime = DateUtil.beginOfMonth(date);
            this.beginTime = endTime;
        } else if (OmConstant.DATA_TYPE_YEAR.equals(dataType)) {
            this.endTime = DateUtil.beginOfYear(date);
            this.beginTime = endTime;
        }
    }

    public Date getEndTime() {
        return endTime;
    }

    public Date getBeginTime() {
        return beginTime;
    }
}
